package com.zhangjianling.premp.realm;

import com.zhangjianling.premp.entity.sys.SysBaseInfo;
import com.zhangjianling.premp.entity.sys.SysMenu;
import com.zhangjianling.premp.entity.sys.SysRole;
import com.zhangjianling.premp.entity.sys.SysUser;
import com.zhangjianling.premp.server.sys.BaseInfoService;
import com.zhangjianling.premp.server.sys.SysUserService;
import com.zhangjianling.premp.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * realm
 *
 * @author zhangjl
 *
 */
public class MyShiroRealm extends AuthorizingRealm {

	@Resource
	private SysUserService sysUserService;

	@Resource
	private BaseInfoService baseInfoService;
	/**
	 * 角色与权限
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals){
		String userName = ShiroUtils.getUser().getUserName();
		SysUser sysUser = sysUserService.findByUserName(userName);
		//SecurityUtils.getSubject().getSession().setAttribute("sysUser",sysUser);
		if (sysUser != null) {
			SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();

			List<SysRole> roles = sysUser.getRoles();
			Set<String> roleIds = new HashSet<String>();
			Set<String> permissions = new HashSet<String>();
			for (SysRole role : roles) {
				// 获得角色
				roleIds.add(role.getName());
				// 获得权限
				List<SysMenu> memus = role.getMemus();
				for (SysMenu sysMenu : memus) {
					if(StringUtils.isNotBlank(sysMenu.getPermission())){
						permissions.add(sysMenu.getPermission());
					}
				}
			}
			// 设置角色
			authorizationInfo.setRoles(roleIds);
			authorizationInfo.setStringPermissions(permissions);
			return authorizationInfo;
		}else{
			return null;
		}
	}

	/**
	 * 认证登录
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		UsernamePasswordToken upToken = (UsernamePasswordToken) token;
		String userName = upToken.getUsername().trim();
		SysUser user = sysUserService.findByUserName(userName);
		if (user != null) {
			if (Boolean.TRUE.equals(user.getLocked())) {
				throw new LockedAccountException("账号已锁定"); // 帐号锁定
			} else {
				SysBaseInfo baseInfo = baseInfoService.getDetail();
				SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(baseInfo.getSalt()),getName());

				return authenticationInfo;
			}
		}else{
			throw new UnknownAccountException();
		}
	}


	@Override
	public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
		super.clearCachedAuthorizationInfo(principals);
	}

	@Override
	public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
		super.clearCachedAuthenticationInfo(principals);
	}

	@Override
	public void clearCache(PrincipalCollection principals) {
		super.clearCache(principals);
	}


	public void clearAllCachedAuthorizationInfo() {
		getAuthorizationCache().clear();
	}

	public void clearAllCachedAuthenticationInfo() {
		getAuthenticationCache().clear();
	}

	/**
	 * 清理所有的缓存
	 */
	public void clearAllCache() {
		clearAllCachedAuthenticationInfo();
		clearAllCachedAuthorizationInfo();
	}

}
